/*
文章评论 api 接口
其中 由于 web 没有接口
UncheckComment CheckComment GetCommentList 函数未使用
*/

package v1

import (
    "net/http"
    "strconv"

    "gitee.com/keeving/quanta-test-cat/middleware"
    "gitee.com/keeving/quanta-test-cat/model"
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"
    "github.com/gin-gonic/gin"
)

// AddComment 新增评论
func AddComment(c *gin.Context) {
    var data model.Comment
    token := c.GetHeader("Authorization")
    username, _ := middleware.GetUserNameFromToken(token)
    userID, _ := model.GetUserIDByUsername(username)
    // _ = c.ShouldBindJSON(&data)
    aid := c.PostForm("aid")
    content := c.PostForm("content")
    file := UploadFile(c)

    data.UserId = uint(userID)
    articleId, _ := strconv.ParseUint(aid, 10, 64)  // tmp
    data.ArticleId = uint(articleId)
    data.Content = content
    data.Img = file

    code := model.AddComment(&data)
    c.JSON(http.StatusOK, gin.H{
        "code":  code,
        "data":    data,
        "msg": errmsg.GetErrMsg(code),
    })
}

// GetComment 获取单个评论信息
func GetComment(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    data, code := model.GetComment(id)
    c.JSON(http.StatusOK, gin.H{
        "status":  code,
        "data":    data,
        "message": errmsg.GetErrMsg(code),
    })
}

// DeleteComment 删除评论
func DeleteComment(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("firstCid"))
    token := c.GetHeader("Authorization")
    username, _ := middleware.GetUserNameFromToken(token)
    userID, _ := model.GetUserIDByUsername(username)
    code := model.DeleteComment(uint(id), userID)
    c.JSON(http.StatusOK, gin.H{
        "code":  code,
        "msg": errmsg.GetErrMsg(code),
        "data": nil,
    })
}

// GetCommentCount 获取评论数量
func GetCommentCount(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    total := model.GetCommentCount(id)
    c.JSON(http.StatusOK, gin.H{
        "total": total,
    })
}

// GetCommentList 后台查询评论列表
func GetCommentList(c *gin.Context) {
    pageSize, _ := strconv.Atoi(c.Query("pagesize"))
    pageNum, _ := strconv.Atoi(c.Query("pagenum"))

    switch {
    case pageSize >= 100:
        pageSize = 100
    case pageSize <= 0:
        pageSize = 10
    }

    if pageNum == 0 {
        pageNum = 1
    }

    data, total, code := model.GetCommentList(pageSize, pageNum)

    c.JSON(http.StatusOK, gin.H{
        "status":  code,
        "data":    data,
        "total":   total,
        "message": errmsg.GetErrMsg(code),
    })

}

// GetCommentListFront 展示页面显示评论列表
func GetCommentListFront(c *gin.Context) {
    id, _ := strconv.Atoi(c.Query("aid"))
    pageSize, _ := strconv.Atoi(c.Query("pagesize"))
    pageNum, _ := strconv.Atoi(c.Query("pagenum"))

    switch {
    case pageSize >= 100:
        pageSize = 100
    case pageSize <= 0:
        pageSize = 10
    }

    if pageNum == 0 {
        pageNum = 1
    }

    datacomment, total, code := model.GetCommentListFront(id, pageSize, pageNum)
    commentNum := model.GetCommentCount(id)

    data := gin.H{
        "commentNum": commentNum,
        "totalPages": total,
        "currentPage": pageNum,
        "firstComments": datacomment,
        
    }
    c.JSON(http.StatusOK, gin.H{
        "code":  code,
        "data":    data,
        "total":   total,
        "msg": errmsg.GetErrMsg(code),
    })

}

// CheckComment 通过审核
func CheckComment(c *gin.Context) {
    var data model.Comment
    _ = c.ShouldBindJSON(&data)
    id, _ := strconv.Atoi(c.Param("id"))

    code := model.CheckComment(id, &data)
    c.JSON(http.StatusOK, gin.H{
        "status":  code,
        "message": errmsg.GetErrMsg(code),
    })
}

// UncheckComment 撤下评论审核
func UncheckComment(c *gin.Context) {
    var data model.Comment
    _ = c.ShouldBindJSON(&data)
    id, _ := strconv.Atoi(c.Param("id"))

    code := model.UncheckComment(id, &data)
    c.JSON(http.StatusOK, gin.H{
        "status":  code,
        "message": errmsg.GetErrMsg(code),
    })
}
